package com.sam.a_jdbctemplate;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;

/**
 * @Author: SamZhao
 * @Description: 演示JDBC模板
 * @Date: 2018/01/13 16:48
 */
public class Demo {

    @Test
    public void dun1(){
        try {
            //0.准备连接池
            ComboPooledDataSource dataSource = new ComboPooledDataSource();

            dataSource.setDriverClass("com.mysql.jdbc.Driver");

            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/springheima");
            dataSource.setUser("root");
            dataSource.setPassword("root");

            //1.创建JDBC模板对象
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            jdbcTemplate.setDataSource(dataSource);

            //2.书写sql 并执行
            String sql = "insert into t_user values(null,'Sam');";

            int result = jdbcTemplate.update(sql);

            System.out.println("result: "+ result);

        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

}
